package com.qdlc.p2p.dal.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.MemberInfosDao;
import com.qdlc.p2p.dal.dto.MemberInfos;

/**
 * @author ygw
 * @version 2.0
 * @date 20151112
 */
@Repository("memberInfosDao")
public class MemberInfosDaoImpl extends BaseDaoImpl<MemberInfos> implements MemberInfosDao {

	/**
	 * 是否存在法人信息
	 * 
	 * @param legalPerson
	 * @return
	 */
	@Override
	public boolean exsitLegalPerson(String legalPerson) {
		String sql = "select * from " + Global.DB_PREFIX
				+ "member_infos m where legal_person=:legalPerSon and status!=0 and status!=2";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("legalPerson", legalPerson);
		List<MemberInfos> memberInfosList = listBySql(sql, params);
		return !CollectionUtils.isEmpty(memberInfosList);
	}

	@Override
	public boolean exsitCertNo(String certNo) {
		String sql = "select * from " + Global.DB_PREFIX
				+ "member_infos m where cert_no=:certNo and status!=0 and status!=2";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("certNo", certNo);
		List<MemberInfos> memberInfosList = listBySql(sql, params);
		return !CollectionUtils.isEmpty(memberInfosList);
	}

	@Override
	public boolean exsitLegalPersonPhone(String legalPersonPhone) {

		String sql = "select * from " + Global.DB_PREFIX
				+ "member_infos m where legal_person_phone=:legalPersonPhone and status!=0 and status!=2";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("certNo", legalPersonPhone);
		List<MemberInfos> memberInfosList = listBySql(sql, params);
		return !CollectionUtils.isEmpty(memberInfosList);
	}

	@Override
	public MemberInfos findByUserId(long userId) {
		MemberInfos member = findObjByProperty("userId", userId);
		return member;
	}

	@Override
	public MemberInfos findByTppId(String tppId) {
		MemberInfos member = findObjByProperty("identityId", tppId);
		return member;
	}

	@Override
	public MemberInfos saveEntity(MemberInfos memeber) {
		MemberInfos member = merge(memeber);
		return member;
	}
}
